package com.yilin.medical.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class JxShareUtils {
	/**
	 * share封装
	 * <p>
	 * Title: ShareUtils
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * <p>
	 * Company: soufun
	 * </p>
	 * 
	 * @author    mwq
	 * @date       2011-9-26
	 */

	Context mContext;
	SharedPreferences sharedPreferences;

	public JxShareUtils(Context mContext) {
		this.mContext = mContext;

	}

	/**
	 * 通过share获取对象
	 * 
	 * @param shareName
	 * @param o
	 * @return
	 */
	public <T> T getEntryForShare(String shareName, Class<T> c) {
		sharedPreferences = mContext.getSharedPreferences(shareName,
				Activity.MODE_PRIVATE);
		T t = null;
		try {
			Map<String, ?> map = sharedPreferences.getAll();
			List<Field> list = getObjFieldWithExtends(c);
			if (map.size() > 0 && list.size() > 0) {
				t = c.newInstance();
				for (Entry<String, ?> et : map.entrySet()) {
					String key = et.getKey();
					Object value = et.getValue();
					for (Field filed : list) {
						String name = filed.getName();
						if (name.equalsIgnoreCase(key)) {
							filed.set(t, value);
							break;
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return t;
	}

	/**
	 * 把对象放进share里
	 * 
	 * @param shareName
	 * @param o
	 */
	public void setShareForEntry(String shareName, Object o) {
		try {
			sharedPreferences = mContext.getSharedPreferences(shareName,
					Activity.MODE_PRIVATE);
			Editor editor = sharedPreferences.edit();
			editor.clear();
			editor.commit();

			List<Field> list = getObjFieldWithExtends(o.getClass());
			for (Field f : list) {
				String name = f.getName();
				try {
					Object rtcs = f.get(o);
					if (rtcs instanceof String) {
						editor.putString(name, (String) rtcs);
					} else if (rtcs instanceof Boolean) {
						editor.putBoolean(name, (Boolean) rtcs);
					} else if (rtcs instanceof Float) {
						editor.putFloat(name, (Float) rtcs);
					} else if (rtcs instanceof Integer) {
						editor.putInt(name, (Integer) rtcs);
					} else if (rtcs instanceof Long) {
						editor.putLong(name, (Long) rtcs);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			editor.commit();
		} catch (Exception e) {
		}

	}

	/**
	 * 返回share文件一个节点的值boolean类型
	 * 
	 * @param shareName
	 * @param key
	 * @return
	 */
	public boolean getBooleanForShare(String shareName, String key) {
		sharedPreferences = mContext.getSharedPreferences(shareName,
				Activity.MODE_PRIVATE);
		return sharedPreferences.getBoolean(key, false);
	}

	/**
	 * 获取对象里边的public属性
	 * 
	 * @param cls
	 * @return
	 */
	public static List<Field> getObjFieldWithExtends(Class<?> cls) {
		if (cls.equals(Object.class)) {
			return null;
		}
		List<Field> list = new ArrayList<Field>();
		Field[] mds = cls.getFields();
		for (int i = 0; i < mds.length; i++) {
			list.add(mds[i]);
		}
		return list;
	}

	/**
	 * 返回share文件一个节点的值
	 * 
	 * @param shareName
	 * @param key
	 * @return
	 */
	public String getStringForShare(String shareName, String key) {
		sharedPreferences = mContext.getSharedPreferences(shareName,
				Activity.MODE_PRIVATE);
		return sharedPreferences.getString(key, "");
	}

	public void setStringForShare(String shareName, String key, String value) {
		Editor editor = mContext.getSharedPreferences(shareName,
				Activity.MODE_PRIVATE).edit();
		editor.putString(key, value);
		editor.commit();
	}

	/**
	 * 把map对象存进share
	 * 
	 * @param shareName
	 * @param map
	 */
	public void setShareForMap(String shareName, HashMap<String, String> map) {
		sharedPreferences = mContext.getSharedPreferences(shareName,
				Activity.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.clear();
		editor.commit();
		for (Entry<String, String> et : map.entrySet()) {
			editor.putString(et.getKey(), et.getValue());
		}
		editor.commit();
	}

	/**
	 * yangfuzhizhuanyong
	 * 
	 * 
	 * 
	 * 
	 * */
	public HashMap<String, String> getMapForShare(String shareName) {
		sharedPreferences = mContext.getSharedPreferences(shareName,
				Activity.MODE_PRIVATE);
		HashMap<String, String> map = new HashMap<String, String>();
		map = (HashMap<String, String>) sharedPreferences.getAll();
		return map;

	}

	/**
	 * 清空share
	 * 
	 * @param shareName
	 * @param map
	 */
	public void clearShare(String shareName) {
		sharedPreferences = mContext.getSharedPreferences(shareName,
				Activity.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.clear();
		editor.commit();
	}
}
